import type { VbenFormProps } from '../form';
import type {
  VxeGridProps,
  VxeGridPropTypes,
  VxeTableGridOptions,
} from '../vxe-table';

class TableConfigBuilder {
  private config: VxeGridProps;

  // 返回配置
  build = (): VxeGridProps => {
    return this.config;
  };

  /**
   * 搜索菜单默认配置
   * @param formOptions
   */
  withSearchForm = (formOptions: VbenFormProps) => {
    this.config = {
      ...this.config,
      formOptions: {
        ...formOptions,
      },
    };
    return this;
  };

  constructor(config: VxeTableGridOptions = {}) {
    this.config = {
      gridOptions: {
        columns: [],
        stripe: true,
        checkboxConfig: {
          highlight: true,
          labelField: 'name',
        },
        height: 'auto',
        keepSource: true,
        toolbarConfig: {
          custom: true,
          export: true,
          refresh: true,
          resizable: true,
          search: true,
          zoom: true,
        },
        ...config,
      },
    };
  }

  /**
   * 表格行操作
   * @param action
   */
  withTableActions(action: VxeGridPropTypes.Column) {
    const columns = this.config.gridOptions?.columns;
    if (
      columns &&
      columns[columns.length - 1]?.title !== '操作'
    ) {
      this.config.gridOptions?.columns?.push(action);
    }
    return this;
  }

  /**
   * 表格标题配置
   * @param tableTitleOptions
   */
  withTableTitle(
    tableTitleOptions: Pick<
      VxeGridProps,
      'tableTitle' | 'tableTitleHelp'
    >,
  ) {
    this.config = {
      ...this.config,
      ...tableTitleOptions,
    };
    return this;
  }
}

export function withDefaultTableConfig(
  tableOptions: VxeTableGridOptions,
) {
  return new TableConfigBuilder(tableOptions);
}

/**
 * 列表排序列
 * @param type
 */
export function serialCol(
  type: VxeGridPropTypes.Column['type'] = 'checkbox',
): VxeGridPropTypes.Column {
  return {
    type,
    width: 70,
  };
}

/**
 * 文本字段表头
 * @param field
 * @param title
 * @param options
 */
export function textCol(
  field: string,
  title: string,
  options?: VxeGridPropTypes.Column,
): VxeGridPropTypes.Column {
  return {
    field,
    title,
    minWidth: 150,
    ...options,
  };
}

/**
 * 字典
 * @param field
 * @param title
 * @param dictType
 * @param options
 */
export function dictCol(
  field: string,
  title: string,
  // dictType: string,
  options?: VxeGridPropTypes.Column,
): VxeGridPropTypes.Column {
  return {
    field,
    title,
    minWidth: 150,
    formatter: ({ cellValue }) =>
      // dictValueMap.value?.[dictType]?.[cellValue] || cellValue,
      `${cellValue}<字典待处理>`,
    ...options,
  };
}

/**
 * 文本字段表头
 * @param field
 * @param title
 */
export function actionCol(
  field: string,
  title: string = '操作',
): VxeGridPropTypes.Column {
  return {
    field,
    title,
    minWidth: 150,
    fixed: 'right',
    slots: {
      default: field,
    },
  };
}
